﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Security.Cryptography;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace ModelLibrary
{  
    public class clsBaseLib
    {
        protected DBUSE.DB db = null;
       
        public clsBaseLib()
        {

        }

        public int iPageRowNum = Convert.ToInt32(ConfigurationManager.AppSettings["PageRowNum"]);

        protected void connectDataBase(string sAppSet)
        {
            db = new DBUSE.DB(sAppSet);
        }

        protected void connectDataBase()
        {
            db = new DBUSE.DB();
        }

        /// <summary>
        /// 获得部门菜单树
        /// </summary>
        /// <returns>部门树信息</returns>
        public DataTable getDeptTree()
        {
            string sSql = "select deptid as id,deptname as name,parentdept as parentid from department order by dbo.getDeptFullPath(deptid)";
            return db.GetTable(sSql);
        }

        #region 判断权限

        /// <summary>
        /// 判断是否有模块权限
        /// </summary>
        /// <param name="iUserId">用户</param>
        /// <param name="iModelId">模块ID</param>
        /// <param name="sFullName">返回模块名</param>
        /// <param name="bRecord">是否记录使用</param>
        /// <returns>有权限返回true</returns>
        public bool checkRightById(int iUserId, int iModelId, ref string sFullName)
        {
            string sSql = string.Format(@"select count(userid) from vUserRight where modelid={0} and userid={1}", iModelId, iUserId);
            bool bResult = Convert.ToInt32(db.ExecuteScalar(sSql)) >= 1;
            sSql = string.Format(@"select fullname from tblmodel where modelid={0}"
                , iModelId);
            sFullName = Convert.ToString(db.ExecuteScalar(sSql));
            return bResult;
        }

        #endregion

        #region 获得部门选择信息

        /// <summary>
        /// 获得部门选择信息
        /// </summary>
        /// <param name="iDeptId">部门ID</param>
        /// <returns></returns>
        public DataSet getDeptSelectInfo(int iDeptId)
        {
            string sProcName = "getDeptInfo";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@DeptId", SqlDbType.Int);
            para[0].Value = iDeptId;
            return db.GetDataSetByProcedure(sProcName, para);
        }

        /// <summary>
        /// 获得子部门
        /// </summary>
        /// <param name="iParDept"></param>
        /// <returns></returns>
        public DataTable getChildrenDept(int iParDept)
        {
            string sSql = string.Format(@"select -1 as id,'请选择下一级部门' as name union select deptid as id,deptname as name from department where parentdept={0}", iParDept);
            return db.GetTable(sSql);
        }

        #endregion

        #region 部门选择树

        /// <summary>
        /// 部门树选择
        /// </summary>
        /// <returns></returns>
        public DataTable getDeptSelectTree()
        {
            string sSql = @"select deptid as id,deptname as name,parentdept as parentid,0 as ischeck,0 as havecheck
            from Department
            order by dbo.getDeptFullPath(deptid)";
            return db.GetTable(sSql);
        }

        #endregion

        #region 公司选择

        /// <summary>
        /// 厂商选择
        /// </summary>
        /// <returns></returns>
        public DataTable getDCompanySelectTree()
        {
            string sSql = @"select companyid as id,companyname as name,0 as parentid,0 as ischeck,0 as havecheck
            from company
            order by companyname";
            return db.GetTable(sSql);
        }

        #endregion

        #region 设备类型选择树

        /// <summary>
        /// 部门树选择
        /// </summary>
        /// <returns></returns>
        public DataTable getDeviceTypeSelectTree()
        {
            string sSql = @"select typeid as id,typename as name,parentid,0 as ischeck,0 as havecheck
            from devicetype
            order by dbo.getDeviceTypeFullPath(typeid)";
            return db.GetTable(sSql);
        }

        /// <summary>
        /// 部门树选择
        /// </summary>
        /// <returns></returns>
        public DataTable getProjectDeviceTree(int iProjectId)
        {
            string sSql = string.Format(@"select typeid as id,typename as name,0 as parentid,0 as ischeck,0 as havecheck
            from devicetype where typeid in(select typeid from deviceproject where projectid={0})", iProjectId);
            return db.GetTable(sSql);
        }

        #endregion

        public DataTable getEnumDdl(int iTypeId)
        {
            string sSql = string.Format(@"select 0 as id,'请选择'+enumtypename as name from enumtype where enumtypeid={0} union
            select enumid as id,enumname as name from enum where enumtypeid='{0}'",iTypeId);
            return db.GetTable(sSql);
        }

        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
            }
        }
    }
}
